package com.myblog.utils;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: Jie
 * @Description: 时间工具
 * @Date: 2019/12/3
 */
public class DateUtils {

    static String nowDate = "";

    static SimpleDateFormat sdfLong = new SimpleDateFormat("yyyy-MM-dd");
    static SimpleDateFormat sdfLongCn = new SimpleDateFormat("yyyy年MM月dd日");
    static SimpleDateFormat sdfLongTime = new SimpleDateFormat("yyyyMMddHHmmss");
    static SimpleDateFormat sdfLongTimePlus = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static SimpleDateFormat sdfShortLongTimePlusCn = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args){
        System.out.println(new DateUtils().getNowYear());
    }

    /**
     * @Descrption: 获取当前时间并格式化
     * @return 2019-12-03
     */
    public static String getDateLong(){
        return getDateTime("yyyy-MM-dd");
    }

    /**
     * @Descrption: 获取日期格式例子：2019-12-03
     * @param date
     * @return
     */
    public static String getDateLong(Date date){
        try {
            if (date != null)
                nowDate = sdfLong.format(date);
            return nowDate;
        }catch (Exception e){
            System.out.println("Error at getDate: " + e.getMessage());
            return "";
        }
    }

    /**
     * @Descrption: 获取当前时间格式化
     * @return 2019年12月3日
     */
    public static String getDateLongCn(){
        return getDateTime("yyyy年MM月dd日");
    }

    /**
     * @descrption: 获取日期格式例子：2019年12月3日
     * @param date
     * @return
     */
    public static String getDateLongCn(Date date){
        try{
            if (date != null)
                nowDate = sdfLongCn.format(date);
            return nowDate;
        }catch (Exception e){
            System.out.println("Error at getDate: " + e.getMessage());
            return "";
        }
    }

    /**
     * @Descrption: 获取当前时间格式话
     * @return 20191203102210
     */
    public static String getNowLongTime(){
        return getDateTime("yyyyMMddHHmmss");
    }

    /**
     * @Descrption: 获取日期格式例子：20191203102210
     * @param date
     * @return
     */
    public static String getNowLongTime(Date date){
        try{
            if (date != null)
                nowDate = sdfLongTime.format(date);
            return nowDate;
        }catch (Exception e){
            System.out.println("Error at getDate：" + e.getMessage());
            return "";
        }
    }

    /**
     * @Descrption: 获取当前时间格式化
     * @return "2019-12-03 10:05:13"
     */
    public static String getNowPlusTime(){
        return getDateTime("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * @Descrption: 获取日期格式例子：2019-12-03 10:05:13
     * @param date
     * @return
     */
    public static String getNowPlusTime(Date date){
        try{
            if (date != null)
                nowDate = sdfLongTimePlus.format(date);
            return nowDate;
        }catch (Exception e){
            System.out.println("Error at getDate:" + e.getMessage());
            return "";
        }
    }

    /**
     * @return "2019年12月3日 10：08"
     */
    public static String getDateShortLongTimeCn(){
        return getDateTime("yyyy年MM月dd日 HH:mm");
    }


    /**
     * @Descrption: 获取日期格式例子：2019年12月3日 10：08
     * @param date
     * @return
     */
    public static String getDateShortLongTimeCn(Date date){
        try{
            if (date != null)
                nowDate = sdfShortLongTimePlusCn.format(date);
            return nowDate;
        }catch (Exception e){
            System.out.println("Error at getDate:" + e.getMessage());
            return "";
        }
    }

    /**
     * @Descrption: 获取当前时间并格式化
     * 使用线程安全得DateTimeFormatter
     * @param time_format 日期格式
     * @return
     */
    private static String getDateTime(String time_format){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter format = DateTimeFormatter.ofPattern(time_format);
        return now.format(format);
    }


    /**-------------------------------------------------------------------------------------------------------------**/

    /**
     * 获取当前年份值
     * @return
     */
    public static String getNowYear(){
        String nowYear = "";
        try {
            String strTemp = getNowLongTime();
            nowYear = strTemp.substring(0, 4);
            return nowYear;
        }catch (Exception e){
            throw e;
        }
    }

    /**
     * 获取当前月份值
     * @return
     */
    public static String getNowMonth(){
        String nowMonth = "";
        try{
            String strTemp = getNowLongTime();
            nowMonth = strTemp.substring(4, 6);
            return nowMonth;
        }catch (Exception e){
            throw e;
        }
    }

    /**
     * 获取当前日期值
     * @return
     */
    public static String getNowDay(){
        String nowDay = "";
        try{
            String strTemp = getNowLongTime();
            nowDay = strTemp.substring(6, 8);
            return nowDay;
        }catch (Exception e){
            throw e;
        }
    }

    /**
     * 得到日期中的 年份
     * @param date 日期
     * @return
     */
    public static String convertDateToYear(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy", new DateFormatSymbols());
        return df.format(date);
    }

    /**
     * 得到日期中 年月 组成的字符串
     * @param date
     * @return
     */
    public static String convertDateToYearMonth(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM", new DateFormatSymbols());
        return df.format(date);
    }

    /**
     * 得到日期中 年月日 组成的字符串
     * @param date
     * @return
     */
    public static String convertDateToYearMonthDay(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd", new DateFormatSymbols());
        return df.format(date);
    }

    /**
     * 得到日期中的 月份
     * @param date
     * @return
     */
    public static String convertDateToMonth(Date date){
        SimpleDateFormat df = new SimpleDateFormat("MM", new DateFormatSymbols());
        return df.format(date);
    }

    /**
     * 得到日期中的 日期值
     * @param date
     * @return
     */
    public static String convertDateToDay(Date date){
        SimpleDateFormat df = new SimpleDateFormat("dd", new DateFormatSymbols());
        return df.format(date);
    }

    /**
     * 获取当前时间时间戳
     * @return
     */
    public long getLongTime(){
        Date date = new Date();
        return date.getTime()/1000;
    }

    /**
     * 得到俩个日期之间相差的天数
     * @param newDate
     * @param oldDate
     * @return newDate-oldDate相差的天数
     */
    public static int daysBetweenDates(Date newDate, Date oldDate){
        int days = 0;
        Calendar calo = Calendar.getInstance();
        Calendar caln = Calendar.getInstance();
        calo.setTime(oldDate);
        caln.setTime(newDate);
        int oday = calo.get(Calendar.DAY_OF_YEAR);
        int nyear = caln.get(Calendar.YEAR);
        int oyear = caln.get(Calendar.YEAR);
        while( nyear > oyear){
            calo.set(Calendar.MONTH, 11);
            calo.set(Calendar.DATE, 31);
            days = days + calo.get(Calendar.DAY_OF_YEAR);
            oyear = oyear + 1;
            calo.set(Calendar.YEAR, oyear);
        }
        int nday = caln.get(Calendar.DAY_OF_YEAR);
        days = days + nday - oday;
        return days;
    }


}
